#include <iostream>
using std::cout;
using std::endl;

//值传递，其本质是进行复制
//值传递是单向的
//在test0空间中a传递给x,b传递给y,x与y发生交换
//但之后没有传回a/b
#if 0
void swap(int x,int y){
    int tmp = x;
    x = y;
    y = tmp;
}
#endif

//地址传递，传给swap的是两个地址，通过指针改变其指向的内容
void swap(int * px,int * py){
    int tmp = *px;
    *px = *py;
    *py = tmp;
}

//引用传递 最简洁高效的方法
//实现的原理：对引用进行操作时等同于对变量本身进行操作
//引用作为参数，不会发生复制（节省空间）
void swap(int & x,int & y){//int & x = a  int & y = b
    int tmp = x;
    x = y;
    y = tmp;
}


void test0(){
    int a = 1,b = 2;
    cout << "a:" << a << ", b:" << b << endl;
    swap(a,b);
    cout << "a:" << a << ", b:" << b << endl;
}

void test1(){
    int a = 1,b = 2;
    cout << "a:" << a << ", b:" << b << endl;
    swap(&a,&b);
    cout << "a:" << a << ", b:" << b << endl;
}

void test2(){
    int a = 1,b = 2;
    cout << "a:" << a << ", b:" << b << endl;
    swap(a,b);
    cout << "a:" << a << ", b:" << b << endl;
}
int main(void){
    /* test0(); */
    /* test1(); */
    test2();
    return 0;
}
